<?php
/**
 * User: Mykola B.
 * Date: 3/2/13
 * Time: 12:13 PM
 */
namespace controllers {
    class AdminController
    {
        private $userContorller;

        private static $templates = array(
            'dbparams' => 'admin/dbparams.html',
            'vparams' => 'admin/vparams.html'
        );

        public function dbParamsGet($f3) {
            if ($f3->get('is_authenticated')) {
                $f3->set('dbparams_saved', false);
                $template = new \Template();
                echo $template->render(self::$templates['dbparams']);
            } else {
                $f3->reroute('/login?url='.$f3->get('SERVER.REQUEST_URI'));
            }
        }

        public function dbParamsPost($f3) {
            if ($f3->get('is_authenticated')) {
                $settings = $f3->get('SETTINGS');
                $dbname = $f3->get('POST.dbname');
                if (!empty($dbname)) {
                    $settings->database->dbname = $dbname;
                    $settings->database->user = $f3->get('POST.user');
                    $settings->database->password = $f3->get('POST.password');
                    $settings->database->host = $f3->get('POST.host');
                    $settings->database->port = $f3->get('POST.port');
                    $settings->save();
                }
                $f3->set('dbparams_saved', true);
                $template = new \Template();
                echo $template->render(self::$templates['dbparams']);
            } else {
                $f3->reroute('/login?url='.$f3->get('SERVER.REQUEST_URI'));
            }
        }

        public function visualisationParamsGet($f3) {
            if ($f3->get('is_authenticated')) {
                $vparam = new \models\VisualisationParamsModel();
                $vparams = $vparam->all();
                $f3->set('vparams_saved', false);
                $f3->set('vparams', $vparams);
                $template = new \Template();
                echo $template->render(self::$templates['vparams']);
            } else {
                $f3->reroute('/login?url='.$f3->get('SERVER.REQUEST_URI'));
            }
        }

        public function visualisationParamsPost($f3) {
            if ($f3->get('is_authenticated')) {
                $vparam = new \models\VisualisationParamsModel();
                $vparams = $vparam->all();
                $vparamsInput = $f3->get('POST.vparams');

                foreach ($vparams as $vparam) {
                    $vparam->key = $vparamsInput[$vparam->id]['key'];
                    $vparam->value = $vparamsInput[$vparam->id]['value'];
                    $vparam->description = $vparamsInput[$vparam->id]['description'];
                    $vparam->save();
                }

                $f3->set('vparams_saved', true);
                $f3->set('vparams', $vparams);
                $template = new \Template();
                echo $template->render(self::$templates['vparams']);
            } else {
                $f3->reroute('/login?url=' . $f3->get('SERVER.REQUEST_URI'));
            }
        }

        public function dbRenameTables($f3) {
            if ($f3->get('is_authenticated')) {
                $settings = $f3->get('SETTINGS');
                $table1 = $settings->table_names->table1;
                $table1new = $f3->get('POST.table1');

                $table2 = $settings->table_names->table2;
                $table2new = $f3->get('POST.table2');

                $table3 = $settings->table_names->table3;
                $table3new = $f3->get('POST.table3');

                if ($table1 !== $table1new) {
                    $settings->table_names->table1 = $f3->get('POST.table1');
                    $settings->save();
                    \DataBase::getConnection()->exec(
                        sprintf('RENAME TABLE %s TO %s;',
                            $table1, $settings->table_names->table1
                        )
                    );
                }

                if ($table2 !== $table2new) {
                    $settings->table_names->table2 = $f3->get('POST.table2');
                    $settings->save();
                    \DataBase::getConnection()->exec(
                        sprintf('RENAME TABLE %s TO %s;',
                            $table2, $settings->table_names->table2
                        )
                    );
                }

                if ($table3 !== $table3new) {
                    $settings->table_names->table3 = $f3->get('POST.table3');
                    $settings->save();
                    \DataBase::getConnection()->exec(
                        sprintf('RENAME TABLE %s TO %s;',
                            $table3, $settings->table_names->table3
                        )
                    );
                }

                $this->dbParamsPost($f3);
            } else {
                $f3->reroute('/login?url=' . $f3->get('SERVER.REQUEST_URI'));
            }
        }
    }
}
